package com.kawaiior.asoulworld.item.weapon;

import com.kawaiior.asoulworld.item.ItemASWeapon;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

import javax.annotation.Nonnull;

import static com.kawaiior.asoulworld.events.ASEventHelper.teleportTo;
import static com.kawaiior.asoulworld.events.ASEventHelper.teleportToEntity;
import static com.kawaiior.asoulworld.util.EntityUtil.getPlayerLookTarget;

public class ItemMiyuWand extends ItemASWeapon {

    private playerPosition pP = new playerPosition();

    public ItemMiyuWand(String name, ToolMaterial material) {
        super(name, material);
        useable = true;
    }

    @Override
    public int getMaxDamage(ItemStack stack) {
        return 0;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
        player.setActiveHand(hand);
        ItemStack stack = player.getHeldItem(hand);
        if (!useable || world.isRemote)
        {return ActionResult.newResult(EnumActionResult.FAIL, stack);}

        if (pP.isCanBack()){
            player.setPositionAndUpdate(pP.posX,pP.posY,pP.posZ);
            pP.setCanNotBack();
            world.createExplosion(player,pP.posX,pP.posY,pP.posZ,3,false);
            player.getCooldownTracker().setCooldown(this,100);
            return super.onItemRightClick(world,player,hand);
        }

        Entity pointedEntity = getPlayerLookTarget(world, player, 128D);

        if (pointedEntity instanceof  EntityLivingBase){
            pP.setBackPos(player.posX,player.posY,player.posZ);
            pP.setCanBack();
            pP.setMove_time(System.currentTimeMillis()+10000L);
//            player.getCooldownTracker().setCooldown(this,120);
            EntityUtil.ApplyBuff((EntityLivingBase) pointedEntity, MobEffects.SLOWNESS,1,10f);
            EntityUtil.ApplyBuff(player, MobEffects.STRENGTH, 1, 5f);
//            teleportTo(pointedEntity.posX,pointedEntity.posY,pointedEntity.posZ,player.posX,player.posY,player.posZ,player);
            teleportToEntity(player, pointedEntity);
        }

        return super.onItemRightClick(world, player, hand);
    }

    @Override
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        if (worldIn.isRemote)return;
        if (pP.isCanBack()){
            if (pP.getMove_time()<=System.currentTimeMillis()){
                pP.setCanNotBack();
                EntityPlayer player = (EntityPlayer)entityIn;
                player.getCooldownTracker().setCooldown(this,180);
//                entityIn.setPositionAndUpdate(pP.posX,pP.posY,pP.posZ);
//                worldIn.createExplosion(entityIn,pP.posX,pP.posY,pP.posZ,2,false);
            }
        }
        super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 10;
    }

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {

    }

    class playerPosition{
        World world;
        boolean canBack = false;
        double posX;
        double posY;
        double posZ;
        long move_time;

        private World getWorld(){
            return world;
        }

        private void setWorld(World world){
            this.world = world;
        }

        private long getMove_time() {
            return move_time;
        }

        private void setMove_time(long move_time) {
            this.move_time = move_time;
        }

        private boolean isCanBack(){
            return canBack;
        }

        private void setCanBack(){
            this.canBack = true;
        }

        private void setCanNotBack(){
            this.canBack = false;
        }

        private void setBackPos(double x, double y, double z){
            this.posX=x;
            this.posY=y;
            this.posZ=z;
        }
    }

}
